package com.ai.dmc.index.dao.jotm;

import javax.naming.NamingException;
import javax.transaction.SystemException;
import org.objectweb.jotm.Current;
import org.objectweb.jotm.Jotm;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;

/**
 * Title： 
 * Description： 
 * Copyright：Copyright (c) 2009
 * Company：亚信科技（中国）有限公司
 * @author zlb, 2016年1月9日
 * @version 1.0
 */
public class JotmFactoryBean implements FactoryBean, DisposableBean {

    private Current jotmCurrent;
    private Jotm jotm;

    public JotmFactoryBean() throws NamingException {
        jotmCurrent = Current.getCurrent();
        if (jotmCurrent == null) {
            jotm = new Jotm(true, false);
            jotmCurrent = Current.getCurrent();
        }
    }

    public void setDefaultTimeout(int defaultTimeout) {
        jotmCurrent.setDefaultTimeout(defaultTimeout);
        try {
            jotmCurrent.setTransactionTimeout(defaultTimeout);
        } catch (SystemException ex) {
        }
    }

    public Jotm getJotm() {
        return jotm;
    }

    public Object getObject() {
        return jotmCurrent;
    }

    public Class<? extends Current> getObjectType() {
        return jotmCurrent.getClass();
    }

    public boolean isSingleton() {
        return true;
    }

    public void destroy() {
        if (jotm != null)
            jotm.stop();
    }

}